package exampcom.qw.permissiontools;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import exampcom.qw.permissiontools.permission.SoulPermission;
import exampcom.qw.permissiontools.permission.bean.Permission;
import exampcom.qw.permissiontools.permission.callbcak.CheckRequestPermissionListener;

public class MainActivity extends FragmentActivity {

    private RxPermissions rxPermissions;

    /**
     * 相机权限请求码
     */
    private final int REQUEST_CODE_PERMISSION_CALL = 11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        rxPermissions = new RxPermissions(this);
        //        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                Disposable disposable = rxPermissions.requestEach(Manifest.permission.CALL_PHONE)
        //                        .subscribe(new Consumer<Permission>() {
        //                            @Override
        //                            public void accept(Permission permission) throws Exception {
        //                                if (permission.granted) {
        //                                    makeCall();
        //                                } else if (permission.shouldShowRequestPermissionRationale) {
        //                                    showExplainDialog();
        //                                } else {
        //                                    Toast.makeText(MainActivity.this, "RX授权失败", Toast.LENGTH_SHORT).show();
        //                                }
        //                            }
        //                        });
        //            }
        //        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
                finish();
            }
        });
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SoulPermission.getInstance()
                                .checkAndRequestPermission(Manifest.permission.CALL_PHONE, new CheckRequestPermissionListener() {
                                    @Override
                                    public void onPermissionOk(Permission permission) {
                                        Toast.makeText(MainActivity.this, "成功1牛批", Toast.LENGTH_SHORT).show();
                                        makeCall();
                                    }

                                    @Override
                                    public void onPermissionDenied(Permission permission) {
                                        Toast.makeText(MainActivity.this, "失败1牛批", Toast.LENGTH_SHORT).show();
                                    }
                                });
                    }
                }).start();

            }
        });
    }

    private boolean checkAndRequestCallPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
                showExplainDialog();
            } else {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA}, REQUEST_CODE_PERMISSION_CALL);
            }
            return false;
        }
        return true;
    }

    private void showExplainDialog() {
        new AlertDialog.Builder(this)
                .setTitle("权限解释")
                .setMessage("因为你拒绝了，所以我们无法帮你联系移动客服了，请你点击授予")
                .setPositiveButton("我知道了", null)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_PERMISSION_CALL);
                    }
                }).create().show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode != REQUEST_CODE_PERMISSION_CALL) {
            return;
        }

        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
        } else {
            makeCall();
        }
    }

    private void makeCall() {
        Uri uri = Uri.parse("tel:10086");
        Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
        startActivity(callIntent);
    }

}
